/**
 * $Revision: 1.0 $
 * $Author: Geln Yang $
 * $Date: Jul 17, 2010 11:46:29 AM $
 *
 * Author: Geln Yang
 * Date  : Jul 17, 2010 11:46:29 AM
 *
 */
package com.gooex.web.action.gbuy.subsc;

import java.util.List;
import org.c4j.string.StringTools;
import org.c4j.system.web.exception.ServiceException;

import com.gooex.domain.base.Category;
import com.gooex.domain.common.Subscription;
import com.gooex.service.base.exception.ExceptionCode;
import com.gooex.service.schedual.ISubscriptionManager;
import com.gooex.web.action.BaseAnonymousAction;

/**
 * @author Geln Yang
 * @version 1.0
 */
public class SubscriptionManageAction extends BaseAnonymousAction {

	private ISubscriptionManager subscriptionManager;

	private String email;

	private String category;

	private String interval;

	private String confirmkey;

	private List<Subscription> subscriptions;

	public String create() throws Exception {
		if (getCurrentUser() != null)
			email = getCurrentUser().getEmail();
		if (StringTools.isBlankOrNull(email))
			return LOGIN;
		return SUCCESS;
	}

	public String save() throws Exception {
		if (email != null && category != null && interval != null) {
			Subscription dbSubscription = subscriptionManager.getSubscription(email, category);

			if (dbSubscription == null) {
				subscriptionManager.create(email, category, interval);
			} else {
				if (getCurrentUser() == null || !email.equals(getCurrentUser().getEmail()))
					throw new ServiceException(ExceptionCode.OPERATION_NOT_ALLOWED);

				dbSubscription.setInterval(interval);
				subscriptionManager.savaOrUpdate(dbSubscription);
			}
		}
		return SUCCESS;
	}

	public String remove() throws Exception {
		if (getCurrentUser() == null)
			return LOGIN;
		if (email != null && category != null) {
			if (email.equals(getCurrentUser().getEmail())) {
				Subscription subscription = new Subscription();
				subscription.setEmail(email);
				subscription.setCategory(category);
				subscriptionManager.remove(subscription);
			}
		}
		return SUCCESS;
	}

	public String listSubscriptions() throws Exception {
		if (getCurrentUser() != null)
			email = getCurrentUser().getEmail();
		if (StringTools.isBlankOrNull(email))
			return LOGIN;
		subscriptions = subscriptionManager.getSubscriptionsByEmail(email);
		return SUCCESS;
	}

	public String confirm() throws Exception {
		if (email != null && category != null && confirmkey != null) {
			Subscription subscription = subscriptionManager.getSubscription(email, category);
			if (subscription == null) {
				throw new ServiceException(ExceptionCode.OPERATION_NOT_ALLOWED);
			}
			if (confirmkey.equals(subscription.getConfirmKey())) {
				subscription.setConfirmed(true);
				subscriptionManager.savaOrUpdate(subscription);
				addActionMessage(getText("subscription.confirm.success"));
				return SUCCESS;
			}
		}
		throw new ServiceException(ExceptionCode.OPERATION_NOT_ALLOWED);
	}

	public Category getCategory(String id) {
		return categoryManager.getCategory(id);
	}

	public List<Subscription> getSubscriptions() {
		return subscriptions;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getInterval() {
		return interval;
	}

	public void setInterval(String interval) {
		this.interval = interval;
	}

	public void setSubscriptionManager(ISubscriptionManager subscriptionManager) {
		this.subscriptionManager = subscriptionManager;
	}

	public void setConfirmkey(String confirmkey) {
		this.confirmkey = confirmkey;
	}

}
